import Vue from 'vue'
export default ({
    namespaced: true,
    state: () => ({
        cartlist: []
    }),
    getters: {
        allinfo(state) {
            let allnum = 0, //商品总数量
                allCheck = true,
                checkNum = 0, //被选中的商品数量
                checkPrice = 0, //被选中的总价格
                allPrice = 0
            let cartlist = state.cartlist

            cartlist.forEach(item => {
                    //  let price = item.minRentAmount + item.deposit
                    let price = parseFloat(item.minRentAmount) + parseFloat(item.deposit)
                    allnum += item.count
                    allPrice += item.count * price
                    if (!item.is_check) {
                        allCheck = false
                    }
                    if (item.is_check) {
                        checkNum += item.count
                        checkPrice += item.count * price
                    }
                })
                // console.log(allnum)
            return {
                allnum,
                allCheck,
                checkNum,
                checkPrice,
                allPrice
            }
        }
    },
    mutations: {
        handleadd(state, obj) { //添加新的商品
            let cartlist = state.cartlist
            let len = cartlist.length

            for (let i = 0; i < len; i++) {
                if (cartlist[i].id == obj.id) {
                    cartlist[i].count++;
                    return false
                }
            }
            Vue.set(obj, 'count', 1) //向响应式对象中，新增一个响应式字段
            Vue.set(obj, 'is_check', true)
            state.cartlist.push(obj)
            console.log(obj);
            console.log(cartlist);
        },
        cartCheckMut(state, idx) { //单选功能
            state.cartlist[idx].is_check = !state.cartlist[idx].is_check
        },
        cartAllCheckMut(state, bool) { //全选控制单选
            let { cartlist } = state
            cartlist.forEach(item => {
                item.is_check = bool
                console.log(item.is_check, 1)
            })
        },
        cartDelMut(state, idx) { //删除商品
            state.cartlist.splice(idx, 1)
        }
    },

})